package com.gxuwz.dao.impl;

import com.gxuwz.dao.InstructorDao;
import com.gxuwz.entity.Department;
import com.gxuwz.entity.Instructor;
import com.gxuwz.entity.vo.InstructorVo;
import com.gxuwz.entity.vo.PageVo;
import com.gxuwz.result.PageInfo;
import com.gxuwz.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class InstructorDaoImpl implements InstructorDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    public PageVo selectInstructorAll(Instructor entity, PageInfo pageInfo) {
        String sql = "select instID,instName,telephone,depName from sys_instructor,sys_department " +
                "where sys_instructor.depID = sys_department.depID and sys_instructor.del_flag = 0 " +
                "and sys_department.del_flag = 0 ";
        BeanPropertyRowMapper<InstructorVo> instructorBeanPropertyRowMapper = new BeanPropertyRowMapper<InstructorVo>(InstructorVo.class);
        List<Object> example = new ArrayList<Object>();
        PageUtil pageUtil = new PageUtil();
        if(entity==null){
            Object[] params = null;
            PageVo pageVo = pageUtil.queryForPage(sql, pageInfo.getPageNum(), pageInfo.getPageSize(), params, instructorBeanPropertyRowMapper,jdbcTemplate);
            return pageVo;
        } else {
            Object[] params;
            if(entity.getInstID() != null && !entity.getInstID().equals("")){
                sql += "and instID like ?";
                example.add("%"+entity.getInstID()+"%");
            }
            if(entity.getInstName() != null && !entity.getInstName().equals("")) {
                sql += "and instName like ?";
                example.add("%"+entity.getInstName()+"%");
            }
            if (entity.getTelephone() != null && !entity.getTelephone().equals("")){
                sql += "and telephone like ?";
                example.add("%"+entity.getTelephone()+"%");
            }
            params = example.toArray();
            PageVo pageVo = pageUtil.queryForPage(sql, pageInfo.getPageNum(), pageInfo.getPageSize(), params, instructorBeanPropertyRowMapper,jdbcTemplate);
            return pageVo;
        }
    }

    public Instructor selectInstructorByID(String depId) {
        String sql = "select * from sys_instructor where instID = ? and del_flag = 0 ";
        BeanPropertyRowMapper<Instructor> instructorBeanPropertyRowMapper = new BeanPropertyRowMapper<Instructor>(Instructor.class);
        Object[] params = new Object[]{depId};
        try{
            Instructor instructor = jdbcTemplate.queryForObject(sql, instructorBeanPropertyRowMapper, params);
            return instructor;
        } catch (Exception ex){
            return null;
        }
    }

    public int insertInstructor(Instructor entity) {
        String sql = "insert into sys_instructor values(?,?,?,?,?,0)";
        Object[] params = new Object[]{entity.getInstID(),entity.getPassword(),entity.getInstName(),entity.getDepID(),entity.getTelephone()};
        int insert = jdbcTemplate.update(sql, params);
        return insert;
    }

    //修改辅导员信息
    public int updateInstructor(Instructor entity, String oldId) {
        String sql = "update sys_instructor set instID = ? , password = ? , instName = ? , depID = ? , telephone = ? " +
                "where instID = ?";
        Object[] params = new Object[]{entity.getInstID(),entity.getPassword(),entity.getInstName(),entity.getDepID(),entity.getTelephone(),oldId};
        int update = jdbcTemplate.update(sql, params);
        return update;
    }

    public int deleteInstructorById(String instId) {
        String sql = "update sys_instructor set del_flag = 1 where instID = ?";
        Object[] params = new Object[]{instId};
        int delete = jdbcTemplate.update(sql, params);
        return delete;
    }

    public List<String> selectInstructorIds() {
        String sql = "select instID from sys_instructor ";
        List<String> ids = jdbcTemplate.queryForList(sql, String.class);
        return ids;
    }

    public List<Department> selectInstructorByDepID(String depId) {
        String sql = "select * from sys_instructor where depID = ? and del_flag = 0";
        BeanPropertyRowMapper<Department> departmentBeanPropertyRowMapper = new BeanPropertyRowMapper<>(Department.class);
        Object[] params = new Object[]{depId};
        List<Department> query = jdbcTemplate.query(sql, departmentBeanPropertyRowMapper, params);
        return query;
    }

    public String selectDepIDByInstID(String instID) {
        String sql = "select depID from sys_instructor where instID = ?";
        Object[] params = new Object[]{instID};
        BeanPropertyRowMapper<Instructor> instructorBeanPropertyRowMapper = new BeanPropertyRowMapper<Instructor>(Instructor.class);
        try{
            Instructor instructor = jdbcTemplate.queryForObject(sql,instructorBeanPropertyRowMapper,params);
            return instructor.getDepID();
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public List<String> selectInstructorIdsNoDel() {
        String sql = "select instID from sys_instructor where del_flag = 0";
        List<String> strings = jdbcTemplate.queryForList(sql, String.class);
        return strings;
    }
}
